package com.ph.shopping.facade.member.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.string.StringTools;
import com.ph.shopping.facade.mapper.MemberMapper;
import com.ph.shopping.facade.member.entity.CertificatesAuth;
import com.ph.shopping.facade.member.service.ICretificateAuthService;
import com.ph.shopping.facade.member.service.IMemberCashService;
import com.ph.shopping.facade.member.vo.MemberInfoByCashVo;

@Component
@Service(version="1.0.0")
public class MemberCashService extends BaseService implements IMemberCashService{

private static final Logger log = LoggerFactory.getLogger(MemberCashService.class);
	
	@Autowired
	private MemberMapper memberMapper;
	
	@Autowired
	private ICretificateAuthService credificatteAuthService;
	
	@Override
	public Result getMemberInfo(Long memberId) {
		log.info("获取会员详情,memberId={}",JSON.toJSON(memberId));
		Result result=getResult(ResultEnum.SUCCESS);
		MemberInfoByCashVo mb= memberMapper.getMemberInfo(memberId);
		if (!org.springframework.util.StringUtils.isEmpty(mb)) {
			//验证会员是否绑定身份证
			if(org.springframework.util.StringUtils.isEmpty(mb.getIdCard())){
				result.setCode(ResultEnum.ID_IS_EMPTY.getCode());
				result.setMessage(ResultEnum.ID_IS_EMPTY.getMsg());
				return result;
			}
			//查询是否已进行实名认证
			Result res=credificatteAuthService.getCretificateAuthInfo(mb.getIdCard());
			CertificatesAuth ca=(CertificatesAuth) res.getData();
			if(org.springframework.util.StringUtils.isEmpty(ca)){
				result.setCode(ResultEnum.ID_NOT_CERTIFIED.getCode());
				result.setMessage(ResultEnum.ID_NOT_CERTIFIED.getMsg());
				return result;
			}
			//判断是否绑定银行卡
			if (org.springframework.util.StringUtils.isEmpty(mb.getBankCard())) {
				result.setCode(ResultEnum.NO_CHECKBIND_EXCEPTION.getCode());
				result.setMessage(ResultEnum.NO_CHECKBIND_EXCEPTION.getMsg());
				return result;
			}
			//判断是否设置支付密码
			if(org.springframework.util.StringUtils.isEmpty(mb.getPayPwd())){
				result.setCode(ResultEnum.PAY_PWD_EMPTY.getCode());
				result.setMessage(ResultEnum.PAY_PWD_EMPTY.getMsg());
				return result;
			}
			//计算真实积分
			mb.setScore(mb.getScore()/10000);
			mb.setBankCard(StringTools.bankNoChange(mb.getBankCard()));
			mb.setRealName(StringTools.realNameChange(mb.getRealName()));
			result.setData(mb);
			return result;
		}else{
			result.setCode(ResultEnum.MEMBERUSER_NON_EXISTENT.getCode());
			result.setMessage(ResultEnum.MEMBERUSER_NON_EXISTENT.getMsg());
			return result;
		}
	}
}
